# coding:utf-8

from .data_collector import DataCollector
from bs4 import BeautifulSoup
import pandas as pd


class WeatherCrawler(DataCollector):
    city = "fuzhou"
    month = 201701
    weather_items = []
    url = ""

    def __init__(self):
        DataCollector.__init__(self)
        self.algorithm_name = "历史天气"
        self.ipynb_template_name = "weather_crawler-template.ipynb"

    def implent(self):
        DataCollector.implent(self)
        self.weather_items = []
        self.url = "https://lishi.tianqi.com/%s/%s.html" % (
            self.city, self.month)
        result = self.http.request(method="GET", url=self.url)
        soup = BeautifulSoup(result.data.decode(), "html.parser")
        
        if soup.find("ul", attrs={"class": "lishitable_content clearfix"}):
            for li in soup.find("ul", attrs={"class": "lishitable_content clearfix"}).find_all("li"):
                line = []
                for li in li.find_all("div"):
                    line.append(li.text.strip())
                if line:
                    self.weather_items.append(line)
        self.df = pd.DataFrame(self.weather_items, columns=[
                               "日期", "最高气温", "最低气温", "天气", "风向"])
        self.df["城市"] = self.city

    def prepareIpynbItems(self):
        DataCollector.prepareIpynbItems(self)
        self.ipynb_items["#city#"] = self.city
        self.ipynb_items["#month#"] = self.month
